//
//  HomeViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/23.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "HomeViewController.h"
#import "HospitalViewController.h"
#import "HomeViewModel.h"
#import "CommonNewsDetailView.h"
#import "CompanyNewsViewController.h"
#import "CompanyViewController.h"
#import "CompanyOfficeDetailView.h"

#import "SectionReusableView.h"
#import "ScrollItem.h"
#import "NewsItem.h"
#import "HCommonItem.h"

#import "TYCOViews.h"
#import "CommonNews.h"

NSString *const newsItemId = @"newsItemId";
NSString *const scrollItemId = @"scrollItemId";

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,TYCOViewsDelegate>

@property (nonatomic,strong) HomeViewModel *viewModel;
@property (nonatomic,strong) TYCOViews *TYCOView;
@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"掌上宏力";
    [self initUI];
    [self loadData];
    
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, 200)];
    [SVProgressHUD show];
}

- (void)initUI {
    self.view.backgroundColor = Main_BGCOLOR;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    [self registerClass];
    [self.view addSubview:_collectionView];
    
}
-(void)loadData{
    _viewModel = [[HomeViewModel alloc] init];
    [_viewModel loadDataArray:^{
        [SVProgressHUD dismiss];
        [_collectionView reloadData];
    }];
}
-(void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD resetOffsetFromCenter];
}
-(void)registerClass{
    //用类名注册
    [_collectionView registerClass:[ScrollItem class] forCellWithReuseIdentifier:scrollItemId];
    [_collectionView registerClass:[HCommonItem class] forCellWithReuseIdentifier:commonItemId];
    [_collectionView registerClass:[NewsItem  class] forCellWithReuseIdentifier:newsItemId];
    
    [_collectionView registerClass:[SectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseHeaderId];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseFooterId];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
}

//item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenW, kScreenW/2);
    }else if(indexPath.section == 1 ){
        return CGSizeMake((kScreenW-3)/4, 90);
    }
    
    return CGSizeMake(kScreenW, 70);
}
-(void)TYCOViewClickedImageAtItem:(NSInteger)item{

}
//跳转方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:{
                 [self pushViewControllerTo:[CompanyOfficeDetailView new]];
//                CompanyViewController *companyV = [[CompanyViewController alloc] init];
//                [self pushViewControllerTo:companyV];
            }
                break;
            case 1:{
                HospitalViewController *hpV = [[HospitalViewController alloc] init];
                [self pushViewControllerTo:hpV];
            }
                break;
            default:
                break;
        }
    }else if (indexPath.section == 2){
        CommonNewsDetailView *detailV = [[CommonNewsDetailView alloc] init];
        detailV.news = _viewModel.newsArray[indexPath.row];
        [self pushViewControllerTo:detailV];
    }
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _viewModel.dataArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section != 2){
        NSDictionary *dic = _viewModel.dataArray[section];
        NSArray *arr = dic[@"contents"];
        return arr.count;
    }
    return  _viewModel.newsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        ScrollItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:scrollItemId forIndexPath:indexPath];
        
        _TYCOView = [[TYCOViews alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenW*0.5)];
        _TYCOView.CO_Interval = 2;
        _TYCOView.TYDelegate = self;
        _TYCOView.images = _viewModel.imageArray;
        
        [cell addSubview:_TYCOView];
        return cell;
    }else if (indexPath.section == 1) {
        HCommonItem *cell = [HCommonItem cellWithCollectionView:collectionView indexPath:indexPath];
        NSDictionary *dic = _viewModel.dataArray[indexPath.section];
        NSArray *arr = dic[@"contents"];
        cell.dic = arr[indexPath.row];
        cell.alpha = 1;
        if (indexPath.row > 1) {
            cell.alpha = 0.3;
        }
        return cell;
    }
    else if (indexPath.section == 2) {
        NewsItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:newsItemId forIndexPath:indexPath];
        [cell updateCellInfoWith:_viewModel.newsArray[indexPath.row] index:indexPath.row];
        return cell;
    }
    return nil;
    
}
//设置headerView的size大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        return CGSizeZero;
    }
    return CGSizeMake(kScreenW, 40);
}
//设置footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeZero;
    }
    return CGSizeMake(kScreenW, 10);
}
//返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        SectionReusableView *headerV = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:reuseHeaderId forIndexPath:indexPath];
        headerV.getLine = YES;
        NSDictionary *dic = _viewModel.dataArray[indexPath.section];
        headerV.title = dic[@"title"];
        [headerV clickedTitle:^{
            CompanyNewsViewController *companyNewsV = [[CompanyNewsViewController alloc] init];
            companyNewsV.type = NewsTypeCommon;
            [self pushViewControllerTo:companyNewsV];
        }];
        return headerV;
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
        UICollectionReusableView *footerV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseFooterId forIndexPath:indexPath];
        footerV.backgroundColor = Main_BGCOLOR;
        return footerV;
    }
    
    return nil;
}

@end
